# -*- coding:utf-8 -*- 
__date__ = '2022/6/24 9:13 下午'

'''
给你一个按 非递减顺序 排序的整数数组 nums，返回 每个数字的平方 组成的新数组，要求也按 非递减顺序 排序。
# 输入：nums = [-7,-3,2,3,11] 输出：[4,9,9,49,121]
'''


class Solution:
  # 双指针的解法
  def sorted_sequences(self, nums):
    result = [-1] * len(nums)
    slow, fast, right = 0, len(nums) - 1, len(nums) - 1
    while slow <= fast:
      lm, rm = nums[slow] ** 2, nums[fast] ** 2
      if lm <= rm:
        result[right] = rm
        fast -= 1
      else:
        result[right] = lm
        slow += 1
      right -= 1
    print(result)


if __name__ == '__main__':
  Solution = Solution()
  nums = [-7, -3, 2, 3, 11]
  Solution.sorted_sequences(nums)
